package ru.timofeyco.justfeelbetter;

import java.util.ArrayList;
import java.util.HashMap;

import ru.timofeyco.justfeelbetter.adapters.DayDrillsAdapter;
import ru.timofeyco.justfeelbetter.model.DayDrillTypeState;
import ru.timofeyco.justfeelbetter.model.Drill;
import ru.timofeyco.justfeelbetter.model.DrillType;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.view.View;
import android.widget.ListView;

public class DayResultView extends View {
	private Rect screenBounds;
	private int productivityPercent;
	private ArrayList<DayDrillTypeState> drillTypeStateList;

	public DayResultView(Context context) {
		super(context);
		
		this.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Dialog dialog = new Dialog(v.getContext());
				dialog.setTitle(v.getContext().getString(R.string.day_dialog_title));
				dialog.setContentView(R.layout.day_result_dialog);
				
				ListView listView = (ListView)dialog.findViewById(R.id.day_result_dialog_listview);
				listView.setAdapter(new DayDrillsAdapter(v.getContext(), R.layout.day_result_dialog_row, DayResultView.this.drillTypeStateList));
				
				dialog.show();
								
			}
		});
	}
	
	public void setScreenBounds(Rect bounds) {
		this.screenBounds = bounds;
	}
	
	public void setProductivityPercent(int productivityPercent) {
		this.productivityPercent = productivityPercent;
	}
	
	public void setDayDrillTypeState(
			ArrayList<DayDrillTypeState> drillTypeStateList) {
		this.drillTypeStateList = drillTypeStateList;
	}
	
	public static int calculatePercent(ArrayList<Drill> drillsList) {
		if (drillsList.size() == 0) {
			return 0;
		}
		
		float summPerc = 0;		
		HashMap<DrillType, Number> proportionsByType = new HashMap<DrillType, Number>();
		for (Drill drill : drillsList) {
			DrillType type = drill.getType();
			Number percent = drill.getCount() / (float)type.getRate();
			if (proportionsByType.containsKey(type)) {
				float oldPercent = proportionsByType.get(type).floatValue();
				float newPercent = percent.floatValue() + oldPercent;
				proportionsByType.put(type, newPercent);
			} else {
				proportionsByType.put(type, percent);
			}			
			summPerc += percent.floatValue();
		}	
		return (int)((summPerc / (float)proportionsByType.keySet().size()) * 100);		
	}
	
	private void drawPercent(Canvas canvas) {		
		Paint paint = new Paint();
    	paint.setAntiAlias(true);
    	paint.setStyle(Paint.Style.FILL_AND_STROKE);
    	paint.setStrokeWidth(1);    	
    	
    	float offset = 50;
    	float screenHeight = (productivityPercent != 0) ? screenBounds.height() - offset: screenBounds.height();
    	float percentScale = productivityPercent / 100.0f;
		Path path = new Path();
		path.moveTo(0, screenHeight);
		path.lineTo(screenBounds.width(), screenHeight);
		path.lineTo(screenBounds.width(), screenHeight - screenHeight * percentScale);
		path.lineTo(0, screenHeight - screenHeight * percentScale);
		path.moveTo(0, screenHeight);
		if (productivityPercent >= 100) {
			paint.setColor(Color.GREEN);
		} else {
			paint.setColor(Color.YELLOW);
		}
		canvas.drawPath(path, paint);
		
		// draw text
		screenHeight = screenBounds.height() - offset;
		Paint textPaint = new Paint();
		textPaint.setColor(Color.RED);
		textPaint.setTextSize(44.0f);
		String text = productivityPercent + "%";
		float textWidth = textPaint.measureText(text);
		canvas.drawText(text, 0, text.length(), 
				(float)screenBounds.width() / 2.0f - (textWidth / 2.0f), 
				(float)screenHeight / 2.0f, textPaint);				
	}
	
	@Override
    protected void onDraw(Canvas canvas) {
    	Paint paint = new Paint();
    	paint.setAntiAlias(true);
    	paint.setStyle(Paint.Style.FILL_AND_STROKE);
    	paint.setStrokeWidth(1);    	
    	
    	drawPercent(canvas);
	}
	
}
